package com.example.seabedmusic.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.seabedmusic.R;
import com.example.seabedmusic.entity.SettingItem;
import java.util.List;

public class SettingsAdapter extends RecyclerView.Adapter<SettingsAdapter.VH> {

    private final List<SettingItem> list;
    private OnSettingItemClickListener listener;

    public interface OnSettingItemClickListener {
        void onSettingItemClick(SettingItem item);
    }

    public SettingsAdapter(List<SettingItem> list) {
        this.list = list;
    }

    public void setOnSettingItemClickListener(OnSettingItemClickListener listener) {
        this.listener = listener;
    }

    static class VH extends RecyclerView.ViewHolder {
        TextView tvTitle;
        ImageView ivIcon;
        View divider;

        VH(View v) {
            super(v);
            tvTitle = v.findViewById(R.id.tvTitle);
            ivIcon = v.findViewById(R.id.ivIcon);
            divider = v.findViewById(R.id.divider);
        }
    }

    @NonNull
    @Override
    public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_setting, parent, false);
        return new VH(v);
    }

    @Override
    public void onBindViewHolder(@NonNull VH holder, int position) {
        SettingItem item = list.get(position);

        holder.tvTitle.setText(item.getTitle());
        holder.ivIcon.setImageResource(item.getIconRes());

        // 根据类型设置不同的文字颜色
        if (item.getType() == SettingItem.TYPE_LOGOUT) {
            holder.tvTitle.setTextColor(holder.itemView.getContext().getColor(android.R.color.holo_red_dark));
        } else {
            holder.tvTitle.setTextColor(holder.itemView.getContext().getColor(android.R.color.black));
        }

        // 隐藏最后一项的分割线
        if (position == list.size() - 1) {
            holder.divider.setVisibility(View.GONE);
        } else {
            holder.divider.setVisibility(View.VISIBLE);
        }

        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onSettingItemClick(item);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
}